package cn.sczhyt.modules.szhy.controller;

import java.io.*;
import java.net.URL;
import java.util.Arrays;

import cn.sczhyt.common.utils.UploadFileUtils;
import cn.sczhyt.modules.app.utils.FileReadOnlineUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.sczhyt.modules.szhy.entity.MeetingFileEntity;
import cn.sczhyt.modules.szhy.service.MeetingFileService;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 会议附件表
 *
 * @author luoshijie
 * @email 1169414426@qq.com
 * @date 2020-09-11 16:50:36
 */
@Api(tags = "PC端会议文件管理")
@RestController
@RequestMapping("szhy/meetingfile")
public class MeetingFileController {
    @Autowired
    private MeetingFileService meetingFileService;

    /**
     * 列表
     */
    @ApiOperation("列表")
    @PostMapping("/list")
    public R list(@RequestParam String meetingId,
                  @RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "20") Integer limit
                  ){
        IPage<MeetingFileEntity>  iPage= meetingFileService.findAll(page,limit,meetingId);
        PageUtils pageUtils = new PageUtils(iPage);
        return R.ok().put("data", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		MeetingFileEntity meetingFile = meetingFileService.getById(id);

        return R.ok().put("data", meetingFile);
    }

    /**
     * 文件在线阅读
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation("PDF文件在线阅读")
    @RequestMapping("/fileReadOnline/{id}")
    public void fileReadOnline(HttpServletResponse response,@PathVariable("id") String id) throws IOException {


        System.out.println("MeetingFileController.fileReadOnline" + "会议文件" + id);
        MeetingFileEntity meetingFile = meetingFileService.getById(id);
        String filePath = meetingFile.getFilePath();
        String fileName = meetingFile.getFileName();
        String fileType = "pdf";
        FileReadOnlineUtils.fileReadOnline(response,filePath,fileName,fileType);
    }

    /**
     * 保存
     */
    @ApiOperation("保存会议文件")
    @PostMapping("/save")
    public R save(@RequestParam("file") MultipartFile[] files,@RequestParam String meetingId){

        meetingFileService.saveMeetingFile(files,meetingId);

        return R.ok();
    }


    /**
     * 保存
     */
    @ApiOperation("保存会议图片")
    @PostMapping("/imageSave")
    public R imageSave(@RequestParam("file") MultipartFile file){

        R r = UploadFileUtils.imageSave(file);

        return r;
    }


    /**
     * 文件下载
     */
    @ApiOperation("文件下载")
    @GetMapping("/download")
    public R download(@RequestParam String id, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {

        meetingFileService.download(id,response,request);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MeetingFileEntity meetingFile){
		meetingFileService.updateById(meetingFile);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @GetMapping("/delete")
    public R delete(@RequestParam String id){

        R r = meetingFileService.delect(id);

        return r;
    }

}
